<?php 

namespace Admin\Controller;
use Admin\Controller\BaseController;
use Home\Controller\IndexController;
/**
 * 专家团队控制器
 * @author 佛祖<381361943@qq.com>
 */
class ExpertController extends BaseController {

	public function __construct() {
		parent::__construct();
	}	
	public function index()
	{
		$expert = M('expert'); // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $expert->order('id desc')->page($_GET['p'].',10')->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $expert->count();
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
	    $Page->setConfig('prev', '上一页');
	    $Page->setConfig('next', '下一页');
	    $Page->setConfig('last', '末页');
	    $Page->setConfig('first', '首页');
	    $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
	    $Page->lastSuffix = false;//最后一页不显示为总页数
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('count',$count );
		$this->display();
	}
	/**
	 * 专家新增
	 * @author 佛祖<381361943@qq.com>
	 */
	public function add()
	{
		if(IS_GET)
		{
			$this->display();
		}
		else
		{
			//print_r(I('post.'));
			$data['name'] = I('post.name');
			$data['img'] = I('post.url');

			$count=strpos($data['img'],get_domain($data['img']));
			$data['img']=substr_replace($data['img'],"",$count,strlen(get_domain($data['img'])));

            $count=strpos($data['bigurl'],get_domain($data['bigurl']));
            $data['bigurl']=substr_replace($data['bigurl'],"",$count,strlen(get_domain($data['bigurl'])));
			$data['content'] = I('post.content');
			$data['video'] = I('post.videoUrl');
			M('expert')->add($data);
			$this->success('添加成功',U('index'));
		}	
	}
	/**
	 * 专家编辑
	 * @author 佛祖<381361943@qq.com>
	 */
	public function save()
	{
		if(IS_GET)
		{
			$id = I('id','');
			$id == ''?$this->error('参数错误'):'';
			$list = M('expert')->find($id);
			$list == ''?$this->error('数据不存在'):'';
			$this->assign('list',$list);
			$this->display();
		}
		else
		{
			/*
			array(6) {
		  ["name"] => string(12) "我是专家"
		  ["url"] => string(62) "http://douxin.com/Uploads/Pictures/20180726/5b596019cffda.jpeg"
		  ["content"] => string(60) "&lt;p&gt;新的内容&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;"
		  ["videoUrl"] => string(51) "pcgbb5k2h.bkt.clouddn.com/video1532583973101904.mp4"
		  ["id"] => string(2) "28"
		  ["__hash__"] => string(65) "3c1f03aced03148284aeb66b10781b46_1aa235039f0059c0b25491c1d7827656"
			}
			*/
			$data = I('post.');
			$id = $data['id'];
			unset($data['id']);
			unset($data['__hash__']);
			$id == ''?$this->error('参数不能为空'):'';
			$count=strpos($data['img'],get_domain($data['img']));
			$data['img']=substr_replace($data['img'],"",$count,strlen(get_domain($data['img'])));

            $count=strpos($data['bigurl'],get_domain($data['bigurl']));
            $data['bigurl']=substr_replace($data['bigurl'],"",$count,strlen(get_domain($data['bigurl'])));
			M('expert')->where(array('id'=>$id))->save($data);
			$this->success('修改成功');
		}	
	}
	/**
	 * 专家删除
	 * @author 佛祖<381361943@qq.com>
	 */
	public function delete()
	{
		$id = I('id');
		$id == ''?$this->error('错误的参数'):1;
		M('expert')->delete($id);
		$this->success('删除成功');
	}
	/**
	 * ajax获取七牛云必要参数
	 * @author 佛祖<381361943@qq.com>
	 */
	public function qiniu()
	{
		echo $this->qiniu_token();
	}
}